package part02_单例模式;

/**枚举
 * @ClassName: SingletonTest02
 * @Author: AllenSun
 * @Date: 2019/12/4 21:41
 */
public class SingletonTest07 {
    public static void main(String[] args) {
        //优缺点跟之前一样，浪费内存
        Singleton07 instance01=Singleton07.INSTANCE;
        Singleton07 instance02=Singleton07.INSTANCE;
        System.out.println(instance01==instance02);
        System.out.println("instance01-hashCode="+instance01.hashCode());
        System.out.println("instance02-hashCode="+instance02.hashCode());

        instance01.sayOK();
        Runtime.getRuntime();
    }
}

/**使用枚举，可实现单例，推荐
 * 这种方式是Effective Java作者Josh Bloch提倡的方式，推荐使用
 * @MethodName:
 * @Author: AllenSun
 * @Date: 2019/12/4 21:58
 */
enum Singleton07{
    INSTANCE;
    public void sayOK(){
        System.out.println("ok-");
    }
}